
Partial Class RIAssessments_Assessments
    Inherits System.Web.UI.Page

    Protected Property GridViewSortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.ParticipantNameLabel.Text = EntityController.GetParticipant().FullName
            Me.BindGrid("AssessmentDate", False)
            Me.NewAssessmentButton.Visible = UserAuthorization.HasInsertPermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.Assessments)
        End If

        ' Set up appropriate navigation menu items while on assessments.  This must be done because
        ' assessments is a separate screen and not a view in the multi view in casePersonal.aspx.
        EntityController.SetSubNavTabsAssessments(Me.EditCaseSubNav)

    End Sub

    Private Sub SetupJavaScript()
        EntityController.JavaScriptAddChildDiv(Me.PageDiv.ClientID, 80)
        EntityController.JavaScriptSetFixedHeight(30)
    End Sub

    Protected Sub BindGrid(ByVal sortExpr As String, ByVal isAscending As Boolean)

        ' List assessments by participant.
        Me.AssessmentGridView.DataSource = Cdm.RIDataCore.Assessments.Assessment.ListByParticipantId(EntityController.GetParticipant.ParticipantId, sortExpr, isAscending)
        Me.AssessmentGridView.DataBind()

    End Sub

    Protected Sub AssessmentGridView_PageIndexChanging(ByVal sender As Object, _
                                                       ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                       Handles AssessmentGridView.PageIndexChanging

        Me.AssessmentGridView.PageIndex = e.NewPageIndex
        Me.BindGrid("AssessmentDate", False)

    End Sub

    Protected Sub AssessmentGridView_RowCommand(ByVal sender As Object, _
                                                ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
                                                Handles AssessmentGridView.RowCommand

        ' Redirect to the details of the selected assessment.
        If e.CommandName = "GoToAssessment" Then
            Response.Redirect("~/RIAssessments/AssessmentDetails.aspx?aid=" & e.CommandArgument)
        End If

    End Sub

    Protected Sub NewAssessmentButton_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles NewAssessmentButton.Click

        ' Determine the ID of the latest assessment that was created in order to pre-load its values
        ' in the details section.
        Dim lastAssessmentId As Integer = 0
        Dim assessment As Cdm.RIDataCore.Assessments.Assessment
        For Each assessment In Cdm.RIDataCore.Assessments.Assessment.ListByParticipantIdSortByAssessmentId(EntityController.GetParticipant.ParticipantId)
            lastAssessmentId = assessment.AssessmentId
            Exit For
        Next

        Response.Redirect("~/RIAssessments/AssessmentDetails.aspx?aid=0&paid=" & lastAssessmentId)

    End Sub

    Protected Sub EditCaseSubNav_ChangeMenu(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles EditCaseSubNav.ChangeMenu

        ' Handle the change menu event in order to redirect back to casePersonal.aspx in the correct location.
        EntityController.ReturnToCasePersonalPage(Me.EditCaseSubNav)

    End Sub

    Protected Sub AssessmentGridView_Sorting(ByVal sender As Object, _
                                             ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
                                             Handles AssessmentGridView.Sorting

        If GridViewSortDirection = SortDirection.Ascending Then
            GridViewSortDirection = SortDirection.Descending
            Me.BindGrid(e.SortExpression, False)
        Else
            GridViewSortDirection = SortDirection.Ascending
            Me.BindGrid(e.SortExpression, True)
        End If

    End Sub

End Class
